package com.dao;

import com.helper.DBHelper;
import com.model.Answer;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AnswerDao {

	private Answer toAnswer(Map<String, Object> data)
	{
		Answer a = new Answer();
		a.setId((Integer)data.get("id"));
		a.setEexamId((Integer)data.get("exam_id"));
		a.setQueNum((Integer)data.get("que_num"));
		a.setUserId((Integer)data.get("user_id"));
		a.setPicId((Integer)data.get("pic_id"));
		a.setType((String)data.get("type"));
		a.setContent((String)data.get("content"));
		a.setComment((String)data.get("comment"));
		return a;
	}
	
	public Boolean add(int examId, int queNum, int userId, int picId, String type, String content, String comment) {
		String sql = "INSERT INTO `answer`(`type`, `exam_id`, `que_num`, `user_id`, `content`, `pic_id`, `comment`) VALUES (?, ?, ?, ?, ?, ?, ?);";
		List<Object> params = new ArrayList<>();
		params.add(type); params.add(examId); params.add(queNum);
		params.add(userId); params.add(content); params.add(picId); params.add(comment);
		DBHelper db = new DBHelper();
		return db.update(sql, params);
	}

	public Boolean update(int examId, int queNum, int userId, int picId, String content, String comment) {
		Answer answer = getByExamNumUser(examId, queNum, userId);
		if(answer == null)
			return false;
		String sql = "UPDATE `answer` SET `content` = ?, `comment` = ?, `pic_id` = ? WHERE `id` = ?;";
		List<Object> params = new ArrayList<>();
		params.add(content);
		params.add(comment);
		params.add(picId);
		params.add(answer.getId());
		DBHelper db = new DBHelper();
		return db.update(sql, params);
	}
	
	public Boolean delete(String id) {
		String sql = "DELETE FROM `answer` WHERE `id`=" + id;
		DBHelper db = new DBHelper();
		return db.update(sql, null);
	}
	
	public Answer getById(int id) {
		Answer result = new Answer();
		String sql = "select * from `answer` where id=" + id;
		DBHelper db = new DBHelper();
		List<Map<String, Object>> data = db.query(sql, null);
		for(Map<String, Object> ele : data) {
			result = toAnswer(ele);
		}
		return result;
	}

	public Answer getByExamNumUser(int examId, int queNum, int userId) {
		Answer result = null;
		String sql = "select * from `answer` where exam_id=? and que_num=? and user_id=?";
		List<Object> params = new ArrayList<>();
		params.add(examId);
		params.add(queNum);
		params.add(userId);
		DBHelper db = new DBHelper();
		List<Map<String, Object>> data = db.query(sql, params);
		for(Map<String, Object> ele : data) {
			result = toAnswer(ele);
		}
		return result;
	}

	public List<Answer> getByUserId(int userId) {
		List<Answer> result = new ArrayList<>();
		String sql = "select * from `answer` where `user_id`=?";
		List<Object> params = new ArrayList<>();
		params.add(userId);
		DBHelper db = new DBHelper();
		List<Map<String, Object>> data = db.query(sql, params);
		for(Map<String, Object> ele : data) {
			result.add(toAnswer(ele));
		}
		return result;
	}

	public List<Answer> getByUserIdAndExamId(int userId, int examId) {
		List<Answer> result = new ArrayList<>();
		String sql = "select * from `answer` where `user_id`=? AND `exam_id`=? ORDER BY `que_num` ASC";
		List<Object> params = new ArrayList<>();
		params.add(userId);
		params.add(examId);
		DBHelper db = new DBHelper();
		List<Map<String, Object>> data = db.query(sql, params);
		for(Map<String, Object> ele : data) {
			result.add(toAnswer(ele));
		}
		return result;
	}

    public List<Answer> getByPaperId(int paperId) {
		List<Answer> result = new ArrayList<>();
		String sql = "select * from `answer` where paper_id=?";
		List<Object> params = new ArrayList<>();
		params.add(paperId);
		DBHelper db = new DBHelper();
		List<Map<String, Object>> data = db.query(sql, params);
		for(Map<String, Object> ele : data) {
			result.add(toAnswer(ele));
		}
		return result;
    }
}
